from flask import Flask
import pymysql
from flask import request, jsonify
from flask_cors import CORS

import asyncio
import time



app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False



CORS(app, cors_allowed_origins="*")


async def async_get_data():
    time.sleep(5)
    #return 'Done!'


@app.route("/data")
async def get_data():

    print('睡10秒')
    #time.sleep(10)
    await async_get_data()
    print('醒了')
    return {'msg':"棒"}


import pymysql
# 连接数据库
# conn = pymysql.connect(host='localhost',
#                        user='root',
#                        password='root',
#                        db='md',
#                        charset='utf8',
#                        cursorclass=pymysql.cursors.DictCursor)
# 创建一个游标
#cursor = conn.cursor()
# 插入数据
# 数据直接写在sql后面


#cursor.execute(sql)



@app.route('/', methods=['GET', 'POST', "PUT", "DELETE"])
async def hello_world():
    #res = db.session.execute("insert into user (`username`) values ('123') ")

    #res = db.session.execute(" select * from user ").fetchall()

    data = request.args.get("id")
    print(data)

    # #data = request.form.get("id")

    # print(data)

    # #return 'Hello Flask'
    # return jsonify({'result': [dict(row) for row in res]})

    #cursor.execute("select * from user")
    #res = cursor.fetchall()

    #print(res)

    return jsonify({'msg':'你好'})


if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0", port=5000)
